<?php

namespace app\wumei\controller;

use app\admin\model\ThemeModel;
use app\wumei\model\WumeiCategoryModel;
use cmf\controller\HomeBaseController;
use think\Db;

//会员jiekou 操作
class AddadminController extends HomeBaseController
{
    static $param;  //请求的参数
    //获取传过来的数据
    public function _initialize()
    {

        $data = $this->request->param();

//        $data =array (
//            'data' => '{&quot;user_login&quot;:&quot;\\u7c73\\u8102\\u53bfwerwea&quot;,&quot;user_pass&quot;:&quot;9d791cba112d39740d1498ec338e193e&quot;,&quot;user_email&quot;:&quot;&quot;,&quot;relation&quot;:1008,&quot;relationname&quot;:&quot;\\u7c73\\u8102\\u53bfwerwea&quot;,&quot;role_id&quot;:[4]}',
//        );

        if(isset($data)&&$data!=[]){
            $data =  htmlspecialchars_decode($data['data']);
            self::$param  = json_decode($data,true);
        }
    }
    //会员添加

    /**
     * 管理员添加提交
     * @adminMenu(
     *     'name'   => '管理员添加提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员添加提交',
     *     'param'  => ''
     * )
     */
    public function addpost()
    {

        if (!empty(self::$param['role_id']) && is_array(self::$param['role_id'])) {
            $role_ids = self::$param['role_id'];
            unset(self::$param['role_id']);


            //dump(self::$param);die;
            self::$param['user_pass'] = cmf_password(self::$param['user_pass']);

                $result = DB::name('user')->insertGetId(self::$param);
                if ($result !== false) {
                    //echo json_encode(self::$param);die;

                    //反向绑定物媒账号
                    $url = config('wumeiurl') . '/WebApi/relation';
                    $data = array('idx' => self::$param['relation'], 'relation' => $result, 'action' => 'add');
                    postCurl($url, $data);
                    //$role_user_model=M("RoleUser");
                    foreach ($role_ids as $role_id) {
                        if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
                            $this->error("为了网站的安全，非网站创建者不可创建超级管理员！");
                        }
                        Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $result]);
                    }

                }else{
                    echo 'user false';die;
                }
        }else{
            echo false;

    }


    }
}